﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Hospital.Common
{
    public class ConvertHelper
    {
        public static string GetString(object value)
        {
            return Convert.ToString(value);
        }

        public static decimal GetDecimal(object value)
        {
            decimal result = 0m;
            decimal.TryParse(GetString(value), out result);
            return result;
        }

        public static int GetInt(object value)
        {
            int result = 0;
            int.TryParse(GetString(value), out result);
            return result;
        }

        public static float GetFloat(object value)
        {
            float result = 0f;
            float.TryParse(GetString(value), out result);
            return result;
        }

        public static Guid GetGuid(object value)
        {
            try
            {
                return new Guid(GetString(value));
            }
            catch { }
            return Guid.Empty;
        }

        public static DateTime GetDateTime(object value)
        {
            DateTime result = DateTime.MinValue;
            DateTime.TryParse(GetString(value), out result);
            return result;
        }

        public static bool GetBool(object value)
        {
            bool result = false;
            bool.TryParse(GetString(value), out result);
            return result;
        }

        public static object GetDataRowValue(DataRow row, string columnname)
        {
            try
            {
                object value = row[columnname];
                if (value == DBNull.Value)
                    return null;
                return value;
            }
            catch (Exception)
            {
                return null;
            }
        }

        public static object GetDataRowValue(DataRow row, int index)
        {
            try
            {
                object value = row[index];
                if (value == DBNull.Value)
                    return null;
                return value;
            }
            catch (Exception)
            {
                return null;
            }
        }

        /// <summary>
        /// 根据Column的设置，取得值
        /// </summary>
        /// <param name="datatype"></param>
        /// <param name="item"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static object GetValue(string datatype, object value)
        {
            switch (datatype)
            {
                case "System.Decimal":
                    return GetDecimal(value);
                case "System.Int32":
                    return GetInt(value);
                case "System.DateTime":
                    return GetDateTime(value);
                case "System.Guid":
                    return GetGuid(value);
                case "System.String":
                default:
                    return GetString(value);
            }
        }

        public static object GetDataRowValue(string datatype, DataRow row, string columnname)
        {
            object value = GetDataRowValue(row, columnname);

            return GetValue(datatype, value);
        }

        public static object GetDataRowValue(string datatype, DataRow row, int index)
        {
            object value = GetDataRowValue(row, index);

            return GetValue(datatype, value);
        }

        /// <summary>
        /// 将DataTable放入一个新的DataSet
        /// </summary>
        /// <param name="dtTable">DataTable</param>
        /// <param name="IsNeedCopy">是否需要复制成新的DataTable</param>
        /// <returns>DataSet</returns>
        public static DataSet PackageDateTable(DataTable dtTable, bool IsNeedCopy)
        {
            DataSet dsSet = new DataSet();
            if (IsNeedCopy)
            {
                dsSet.Tables.Add(dtTable.Copy());
            }
            else
                dsSet.Tables.Add(dtTable);

            return dsSet;
        }

        /// <summary>
        /// 复制DataTable
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static DataTable CopyDataTable(DataTable dt)
        {
            DataTable copyTable = dt.Clone();
            foreach (DataRow row in dt.Rows)
            {
                DataRow copyRow = copyTable.NewRow();
                for (int i = 0; i < row.ItemArray.Length; i++)
                {
                    copyRow[i] = row[i];
                }
                copyTable.Rows.Add(copyRow);
            }
            return copyTable;
        }

        /// <summary>
        /// DataTable增加一行，其值与DataRow中相同
        /// </summary>
        /// <param name="row">DataRow</param>
        /// <param name="dtDestination">DataTable</param>
        public static void CopyDataRow(DataRow row, DataTable dtDestination)
        {
            DataRow newDR = dtDestination.NewRow();
            newDR.ItemArray = row.ItemArray;
            dtDestination.Rows.Add(newDR);
        }


        /// <summary>
        /// 获取点分隔的字符串的最后一节
        /// </summary>
        /// <param name="sSource"></param>
        /// <returns></returns>
        public static string GetPartFromLastDot(string sSource)
        {
            if (!string.IsNullOrEmpty(sSource))
            {
                if (sSource.IndexOf('.') > -1)
                {
                    int lastindex = sSource.LastIndexOf('.');
                    if (lastindex == sSource.Length - 1)
                        return string.Empty;
                    else
                        return sSource.Substring(lastindex + 1);
                }
            }

            return sSource;
        }

        //public static DataTable IListToDataSet(IList<T> list)
        //{
        //    if (list == null || list.Count <= 0)
        //    {
        //        return null;
        //    }
        //    DataTable dt = new DataTable(typeof(T).Name);
        //    DataColumn column;
        //    DataRow row;

        //    System.Reflection.PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

        //    foreach (T t in list)
        //    {
        //        if (t == null)
        //        {
        //            continue;
        //        }
        //        row = dt.NewRow();
        //        for (int i = 0, j = propertyInfo.Length; i < j; i++)
        //        {
        //            System.Reflection.PropertyInfo pInfo = propertyInfo[i];
        //            string name = pInfo.Name;
        //            if (dt.Columns[name] == null)
        //            {
        //                column = new DataColumn(name, pInfo.PropertyType);
        //                dt.Columns.Add(column);
        //            }
        //            row[name] = pInfo.GetValue(t, null);
        //        }
        //        dt.Rows.Add(row);
        //    }
        //    return dt;
        //}
    }
}
